﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;

namespace yeqw.FrameWork
{
    public static class ObjectExtensions
    {
        /// <summary>
        /// ToInt32
        /// </summary>
        /// <returns></returns>
        public static int ToInt16(this object obj)
        {
            lock (typeof(ObjectExtensions))
            {
                return Convert.ToInt16(obj);
            }
        }
        /// <summary>
        /// ToInt32
        /// </summary>
        /// <returns></returns>
        public static int ToInt32(this object obj)
        {
            lock (typeof(ObjectExtensions))
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// ToInt32
        /// </summary>
        /// <returns></returns>
        public static long ToInt64(this object obj)
        {
            lock (typeof(ObjectExtensions))
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// ToDateTime
        /// </summary>
        /// <returns></returns>
        public static DateTime ToDateTime(this object obj)
        {
            lock (typeof(ObjectExtensions))
            {
                return Convert.ToDateTime(obj);
            }
        }
        /// <summary>
        /// IsNull
        /// </summary>
        /// <returns></returns>
        public static bool IsNull(this object obj)
        {
            lock (typeof(ObjectExtensions))
            {
                return obj == null;
            }
        }

        /// <summary>
        /// IsNullOrEmpty
        /// </summary>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this object obj)
        {
            lock (typeof(ObjectExtensions))
            {
                return obj == null || obj.ToString().IsNullOrEmpty();
            }
        }
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static MemoryStream Serialize(this object obj)
        {
            lock (typeof(ObjectExtensions))
            {
                return SerializeHelper.Serialize(obj);
            }
        }
    }
}
